/*
 * Alloy Analyzer
 * Copyright (c) 2002 Massachusetts Institute of Technology
 *
 * The Alloy Analyzer is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * The Alloy Analyzer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the Alloy Analyzer; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

package alloy.bool;

import alloy.util.Dbg;

/**
 * Holds the current value of a particular
 * switch.  The value changes when a {@linkplain GBF_V visitor}
 * traversing a Boolean DAG visits a {@linkplain SetVarExpr switch setter} node
 * {@linkplain SetVarExpr#var associated} with this switch.
 *
 * @see BooleanFormula#makeSwitchableConst
 * @see BooleanFormula#makeSwitch
 */
public class BoolSwitch {
    /**
     * The current value of this switch.  Meaningless unless {@link #_isSet} is true.
     * Written when a {@linkplain GBF_V visitor} visits a {@linkplain SetVarExpr switch setter}
     * node, read when the visitor visits a {@linkplain VarConst switchable constant} node.
     */
    private long _switchValue;

    /**
     * The name of the switch -- used only for debugging purposes.
     * Typically the name of the quantified Alloy variable
     * {@linkplain alloy.transl.TranslVisitor#_var2switch associated}
     * with this switch.
     */
    private String _name;

    /**
     * Whether this switch has been set.  Used for sanity checking.  The switch should only
     * be set while a {@linkplain GBF_V visitor} is traversing the Boolean DAG below a
     * {@linkplain SetVarExpr switch setter} node that {@linkplain SetVarExpr#var sets} this switch.
     */
    private boolean _isSet;

    /**
     * Construct a new switch.  The name is recorded for debugging purposes only.
     */
    public BoolSwitch(String name_) { _name = name_; }

    /**
     * Set the value of this switch.  This is done by {@linkplain GBF_V visitors} when
     * they visit a {@linkplain SetVarExpr switch setter} node {@linkplain SetVarExpr#var associated} with this
     * switch.
     */
    public void setVarValue(long switchValue_) {
    if (_isSet)
        //System.out.println("already set: " + this);
        Dbg.chk(!_isSet);
        _switchValue = switchValue_;
        _isSet = true;
    }

    /**
     * Mark this switch as not having any value.  This is done by {@linkplain GBF_V visitors} when
     * they backtrack above a {@linkplain SetVarExpr switch setter} node that set this switch.
     * Used for debugging purposes -- if we visit any {@linkplain VarConst switchable constant} nodes
     * that {@linkplain VarConst#var depend} on this switch while the switch is not {@linkplain #setVarValue set},
     * that is an error.
     */
    public void clearVarValue() {
    Dbg.chk(_isSet);
    _isSet = false;
    }

    /**
     * Return current value of the switch.  Used by {@linkplain GBF_V visitors} when they visit
     * a {@linkplain VarConst switchable constant} and need to determine its current truth value.
     * Each switchable constant reads one bit in the current value of a particular {@link BoolSwitch}
     * to determine its current constant Boolean truth value.
     */
    public long getVarValue() {
    if (!_isSet)
        //System.out.println("unset switch: " + _name);
        Dbg.chk(_isSet);
        return _switchValue;
    }

    public String toString() { return "BSW:" + _name; }
}
